import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class AreaService extends BaseService<any> {
  private readonly logger = new Logger(AreaService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getListByPid
   * 对应 PHP: AreaService_admin::getListByPid()
   * 逻辑类型: undefined - undefined
   */
  async getListByPid(pid: number) {
    // 基于PHP真实逻辑: getListByPid
    // PHP原文: $cache_name = self::$cache_tag_name.'_pid_'.$pid;         return cache_remember(             $cache_name,             function() use($pid) {          ...
const cache_name = self.cache_tag_name + '_pid_'.pid;
        return cache_remember(
            cache_name,
            function() use(pid) {
                return this.model.where([['pid', '=', pid))).field('id, pid, name, shortname, longitude, latitude, level, sort, status').select().toArray(];
  }

  /**
   * getAreaTree
   * 对应 PHP: AreaService_admin::getAreaTree()
   * 逻辑类型: undefined - undefined
   */
  async getAreaTree(level: number) {
    // 基于PHP真实逻辑: getAreaTree
    // PHP原文: $cache_name = self::$cache_tag_name.'_tree_'.$level;         return cache_remember(             $cache_name,             function() use($level) {     ...
const cache_name = self.cache_tag_name + '_tree_'.level;
        return cache_remember(
            cache_name,
            function() use(level) {
                const list = this.model.where([['level', '<=', level))).field('id, pid, name, shortname, longitude, latitude, level, sort, status').select().toArray(];
                return list_to_tree(list);
  }

  /**
   * getAreaByAreaCode
   * 对应 PHP: AreaService_admin::getAreaByAreaCode()
   * 逻辑类型: undefined - undefined
   */
  async getAreaByAreaCode(id: any) {
    // 基于PHP真实逻辑: getAreaByAreaCode
    // PHP原文: $cache_name = self::$cache_tag_name.'_area_'. $id;         return cache_remember(             $cache_name,             function() use($id) {          ...
const cache_name = self.cache_tag_name + '_area_'. id;
        return cache_remember(
            cache_name,
            function() use(id) {
                const level = [1 => 'province', 2 => 'city', 3 => 'district'];
                const tree = [];
                const area = this.model.where([ ['id', '=', id) )).field('id,level,pid,name').findOrEmpty(];

                if (!area.isEmpty()) {
                    tree[ level[ area.level ] ] = area.toArray(];

                    while (area.level > 1) {
                        const area = this.model.where([ ['id', '=', area.pid ) )).field('id,level,pid,name').findOrEmpty(];
                        if (!area.isEmpty()) {
                            tree[ level[ area[ 'level' ] ] ] = area.toArray(;
  }
                }
                return tree;
  }

  /**
   * getAddress
   * 对应 PHP: AreaService_admin::getAddress()
   * 逻辑类型: undefined - undefined
   */
  async getAddress(address: string) {
    // 基于PHP真实逻辑: getAddress
    // PHP原文: $map = (new ConfigService())->getMap();         $url = "https://apis.map.qq.com/ws/geocoder/v1/?address=".$address."&key=".$map['key'];         $curl ...
const map = this.configService.getMap(];
        const url = "https://apis.map.qq.com/ws/geocoder/v1/?address=".address + '&key=".map.key;
        const curl = curl_init(];
        curl_setopt(curl, CURLOPT_RETURNTRANSFER, true];
        curl_setopt(curl, CURLOPT_HEADER, 0];
        curl_setopt(curl, CURLOPT_URL, url];
        curl_setopt(curl, CURLOPT_TIMEOUT, 1];
        curl_setopt(curl, CURLOPT_SSL_VERIFYPEER, false];
        curl_setopt(curl, CURLOPT_SSL_VERIFYHOST, false];

        const res = curl_exec(curl];
        const res = json_decode(res, true);
        if (res) {
            curl_close(curl);
            return res;
  }

  /**
   * getAddressInfo
   * 对应 PHP: AreaService_admin::getAddressInfo()
   * 逻辑类型: undefined - undefined
   */
  async getAddressInfo(location: string) {
    // 基于PHP真实逻辑: getAddressInfo
    // PHP原文: $map = (new ConfigService())->getMap();         $url = "https://apis.map.qq.com/ws/geocoder/v1/?location=".$location."&key=".$map['key'];         $cur...
const map = this.configService.getMap(];
        const url = "https://apis.map.qq.com/ws/geocoder/v1/?location=".location + '&key=".map.key;
        const curl = curl_init(];
        curl_setopt(curl, CURLOPT_RETURNTRANSFER, true];
        curl_setopt(curl, CURLOPT_HEADER, 0];
        curl_setopt(curl, CURLOPT_URL, url];
        curl_setopt(curl, CURLOPT_TIMEOUT, 1];
        curl_setopt(curl, CURLOPT_SSL_VERIFYPEER, false];
        curl_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);

        // 设置 Referer 头（需替换为你的授权域名）
        curl_setopt(curl, CURLOPT_HTTPHEADER, [
            'Referer: ' . this.request.domain()
        ]];

        const res = curl_exec(curl];
        const res = json_decode(res, true);
        if (res) {
            curl_close(curl);
            return res;
  }

  /**
   * getAreaId
   * 对应 PHP: AreaService_admin::getAreaId()
   * 逻辑类型: undefined - undefined
   */
  async getAreaId(name: any, level: any) {
    // 基于PHP真实逻辑: getAreaId
    // PHP原文: $field = 'id';         $info = $this->model->field($field)->where([['name', 'like', '%' . $name . '%' ], ['level', '=', $level]])->findOrEmpty()->toAr...
const field = 'id';
        const info = this.model.field(field).where([['name', 'like', '%' . name  + '%' ], ['level', '=', level))).findOrEmpty().toArray(];
        return info.id;
    }

    /**
     * 获取地址名称
     */
    async getAreaName(id){
        const info = this.model.field("name").where([['id', '=', id ))).findOrEmpty().toArray(];
        return info.name;
    }

}
  }

  /**
   * getAreaName
   * 对应 PHP: AreaService_admin::getAreaName()
   * 逻辑类型: undefined - undefined
   */
  async getAreaName(id: any) {
    // 基于PHP真实逻辑: getAreaName
    // PHP原文: $info = $this->model->field("name")->where([['id', '=', $id ]])->findOrEmpty()->toArray();         return $info['name'];     }  }...
const info = this.model.field("name").where([['id', '=', id ))).findOrEmpty().toArray(];
        return info.name;
    }

}
  }
}
